package com.baiyun.Ui;

import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.*;

public class RegisterJFrame extends JFrame implements MouseListener {
    // 用户输入的注册信息
    private JTextField jTextFieldUsername;
    private JTextField jTextFieldPassword;
    private JTextField jTextFieldAgainPassword;

    // 按钮
    private JButton jButtonRemoveAllText;
    private JButton jButtonRegister;

    public RegisterJFrame() {
        // 初始化窗体
        initJFrame();

        // 初始化界面
        initImage();

        // 点X后让界面消失
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        // 保证界面不消失
        this.setVisible(true);
    }

    private void initImage() {
        // 创建用户名文字
        JLabel jLabelName = new JLabel(new ImageIcon("image/register/注册用户名.png"));
        jLabelName.setBounds(71, 148, 79, 17);
        this.getContentPane().add(jLabelName);

        // 创建用户名的文本输入框
        jTextFieldUsername = new JTextField();
        jTextFieldUsername.setBounds(180, 149, 200, 30);
        this.getContentPane().add(jTextFieldUsername);

        // 添加密码文字
        JLabel jLabelPassword = new JLabel(new ImageIcon("image/register/注册密码.png"));
        jLabelPassword.setBounds(81, 199, 64, 16);
        this.getContentPane().add(jLabelPassword);

        // 创建密码输入框
        jTextFieldPassword = new JTextField();
        jTextFieldPassword.setBounds(180, 200, 200, 30);
        this.getContentPane().add(jTextFieldPassword);

        // 添加再次输入密码文字
        JLabel jLabelAgainPassword = new JLabel(new ImageIcon("image/register/再次输入密码.png"));
        jLabelAgainPassword.setBounds(66, 247, 97, 17);
        this.getContentPane().add(jLabelAgainPassword);

        // 创建再次输入密码输入框
        jTextFieldAgainPassword = new JTextField();
        jTextFieldAgainPassword.setBounds(180, 248, 200, 30);
        this.getContentPane().add(jTextFieldAgainPassword);

        // 创建注册按钮
        jButtonRegister = new JButton();
        jButtonRegister.setIcon(new ImageIcon("image/register/注册按钮.png"));
        jButtonRegister.setBounds(58, 300, 128, 47);
        jButtonRegister.setBorderPainted(false);
        jButtonRegister.setContentAreaFilled(false);
        jButtonRegister.addMouseListener(this);
        this.getContentPane().add(jButtonRegister);

        // 创建重置按钮
        jButtonRemoveAllText = new JButton();
        jButtonRemoveAllText.setIcon(new ImageIcon("image/register/重置按钮.png"));
        jButtonRemoveAllText.setBounds(250, 300, 128, 47);
        jButtonRemoveAllText.setBorderPainted(false);
        jButtonRemoveAllText.setContentAreaFilled(false);
        jButtonRemoveAllText.addMouseListener(this);
        this.getContentPane().add(jButtonRemoveAllText);

        // 添加背景
        JLabel jLabelBg = new JLabel(new ImageIcon("image/register/background.png"));
        jLabelBg.setBounds(0, 0, 470, 390);
        this.getContentPane().add(jLabelBg);
    }

    private void initJFrame() {
        this.setSize(488, 430);
        this.setTitle("拼图 注册");
        this.setAlwaysOnTop(true);
        this.setLocationRelativeTo(null);
        this.setLayout(null);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
    }

    @Override
    public void mousePressed(MouseEvent e) {
        Object source = e.getSource();
        //注册按钮
        if (source == jButtonRegister) {
            registerUser(); // 处理注册功能
        }
        //重置按钮
        else if (source == jButtonRemoveAllText) {
            clearAllText(); // 清空输入框
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    // 注册用户
    private void registerUser() {
        String username = jTextFieldUsername.getText();
        String password = jTextFieldPassword.getText();
        String againPassword = jTextFieldAgainPassword.getText();

        // 输入校验
        if (username.isEmpty() || password.isEmpty() || againPassword.isEmpty()) {
            JOptionPane.showMessageDialog(this, "所有输入框均不能为空！", "错误", JOptionPane.ERROR_MESSAGE);
            return;
        }
        if (!password.equals(againPassword)) {
            JOptionPane.showMessageDialog(this, "两次密码输入不一致！", "错误", JOptionPane.ERROR_MESSAGE);
            return;
        }

        // 文件路径
        String filePath = "userInfo.txt";
        File file = new File(filePath);

        try {
            // 检查用户名是否已存在
            if (file.exists()) {
                BufferedReader reader = new BufferedReader(new FileReader(file));
                String line;
                while ((line = reader.readLine()) != null) {
                    String[] userData = line.split("=");
                    if (userData[0].equals(username)) {
                        JOptionPane.showMessageDialog(this, "用户名已存在！", "错误", JOptionPane.ERROR_MESSAGE);
                        reader.close();
                        return;
                    }
                }
                reader.close();
            }

            // 写入新用户数据
            BufferedWriter writer = new BufferedWriter(new FileWriter(file, true));
            writer.write(username + "=" + password);
            writer.newLine();
            writer.close();

            JOptionPane.showMessageDialog(this, "注册成功！", "提示", JOptionPane.INFORMATION_MESSAGE);

            // 注册成功后关闭当前页面
            this.dispose();

        } catch (IOException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(this, "注册失败，文件操作异常！", "错误", JOptionPane.ERROR_MESSAGE);
        }
    }

    // 清空输入框
    private void clearAllText() {
        jTextFieldUsername.setText("");
        jTextFieldPassword.setText("");
        jTextFieldAgainPassword.setText("");
    }
}
